package com.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.catalina.Session;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bean.User;
import com.service.PermissionService;
import com.service.UserService;
import com.util.TimeUtil;

/**
 * @author author : 张鹏
 * @date createTime：2018年1月10日 下午3:39:10
 * @Description: 该类的功能描述
 */
@Controller
@RequestMapping("user")
public class UserController {
	@Autowired
	private UserService userService;
	@Autowired
	private PermissionService permissionService;
	
	

	User user;

	/**
	 * 登录 ajax
	 * 
	 * @param username
	 * @param password
	 * @param response
	 */
	@RequestMapping("login")
	@ResponseBody
	public int login(String username, String password,
			HttpServletRequest request) {
		user = new User();
		user.setUsername(username);
		if (password!=null) {
			user.setPassword(DigestUtils.md5Hex(password));
		}else {
			user.setPassword(password);
		}
		


		User user2 = userService.login(user);
		if (user2 != null) {
			HttpSession session = request.getSession();
			session.setAttribute("userInfo", user2);
			session.setAttribute("userPermissionInfo",
					permissionService.getPermissionByRoleId(user2.getRoleId()));
			session.setAttribute("permissionInfo",
				permissionService.getAllPermission());
			if ("供应商".equals(user2.getRole().getRoleName())) {
				return 2;
			}else {
				return 1;
			}
		}
		
		return 0;
	}

	@RequestMapping("logout")
	public String logout(HttpServletRequest request) {
		request.getSession().setAttribute("userInfo", null);
		request.getSession().setAttribute("permissionInfo", null);
		request.getSession().setAttribute("userPermissionInfo", null);
		return "login";
	}

	@RequestMapping("logoutAjax")
	@ResponseBody
	public int logoutAjax(HttpServletRequest request) {
		request.getSession().setAttribute("user", null);
		return 1;
	}

	/**
	 * 获取所有的厨师 暂时用查询所有的用户
	 * 
	 * @return
	 */
	@RequestMapping("getAllCookAjax")
	@ResponseBody
	public List<User> getAllCookAjax() {
		return userService.getAllCook();
	}

	/**
	 * 删除用户
	 * 
	 * @return
	 */
	@RequestMapping("deleteUserAjax")
	@ResponseBody
	public int deleteUserAjax(int userId) {
		return userService.deleteUser(userId);
	}
	
	/*
	 * 查询所有用户
	 */
	@RequestMapping("getAllUserAjax")
	@ResponseBody
	public List<User> getAllUserAjax() {
		return userService.getAllUser();
	}

	/*
	 * 查询所有供应商
	 */
	@RequestMapping("getAllSupplierAjax")
	@ResponseBody
	public List<User> getAllSupplierAjax() {
		return userService.getAllSupplier();
	}

	/**
	 * 查询单个用户 并传入返回地址jsp returnTo
	 * 
	 * @param userId
	 * @param returnTo
	 * @param request
	 * @return
	 */
	@RequestMapping("getUserByPrimaryKeyReturnTo")
	public String getSupplierByPrimaryKey(int userId, String returnTo,
			HttpServletRequest request) {
		request.setAttribute("user", userService.getUserByPrimaryKey(userId));
		return returnTo;
	}

	@RequestMapping("getUserByPrimaryKeyAjax")
	public User getUserByPrimaryKeyAjax(int userId) {
		return userService.getUserByPrimaryKey(userId);
	}

	@RequestMapping("createNewUserAjax")
	@ResponseBody
	public int createNewUserAjax(User user) {
		user.setState(1);
		user.setCreateTime(TimeUtil.formatTime());
		if (user.getPassword()!=null) {
			user.setPassword(DigestUtils.md5Hex(user.getPassword()));
		}
		return userService.createNewUser(user);
	}

	@RequestMapping("updateUserAjax")
	@ResponseBody
	public int updateUserAjax(User user) {
		return userService.updateUser(user);
	}

}
